<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Service\Product;

use App\Models\ProductOrder;
use App\Models\ProductShipment;
use App\Models\User;
use App\Service\AdvanceCard\AdvanceCardService;
use App\Service\BaseService;
use App\Service\Setting\UserSettingService;
use App\Service\User\PerformanceService;
use App\Service\User\PointAccountServices;
use App\Service\User\UserServices;
use Illuminate\Support\Facades\DB;

class ProductOrderService extends BaseService
{
    /**
     * 下单.
     * @param mixed $productOrder
     * @param mixed $method
     */
    public function placeAnOrder(User $user, $productOrder, $method)
    {
        DB::transaction(function () use ($user, $productOrder, $method) {
            if ($productOrder->status != 3) {
                $productOrder->status = 3;
                $productOrder->save();
            }
            ProductShipment::query()->create([
                'user_id' => $user->id,
                'order_id' => $productOrder->refresh()->id,
                'user_address_id' => $user->address->id,
                'status' => 0,
                'method' => $method,
            ]);
        });

//        $levelSetting = UserServices::make()->amountUpdateUserLevel($productOrder->total_amount, $user);
//
//        //等级调整
//        if ($levelSetting) {
            //赠送积分
            $totalAmount = $productOrder->total_amount;
            $Setting = UserSettingService::make()->matchingLevel($totalAmount);
            $point = bcmul($Setting->multiple, $productOrder->total_amount);
            PointAccountServices::make()->recharge($user->id, $point, "商品购买赠送积分:{$point}");
//        }
        //直推奖励10% group_reward_points
        PerformanceService::make()->directPushReward($user, $productOrder->total_amount);
    }

    /**
     * 退款.
     */
    public function refund(ProductOrder $order)
    {
    }

    /**
     * 发货.
     */
    public function shipment(ProductOrder $order)
    {
    }
}
